package com.geeklan.smart.ui.viewmodel.user;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.LoginResponse;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.repository.RepositoryCallback;
import com.geeklan.smart.data.repository.user.AccountRepository;
import com.geeklan.smart.utils.AppUtils;
import com.geeklan.smart.utils.SharedPreferencesUtil;

public class UserViewModel extends ViewModel {
    private final AccountRepository userRepository;
    private MutableLiveData<ApiResponse<LoginResponse>> userLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> registerLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> updatePwdLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> resetAccountLiveData = new MutableLiveData<>();

    public MutableLiveData<ApiResponse> getUpdatePwdLiveData() {
        return updatePwdLiveData;
    }

    public MutableLiveData<ApiResponse> getResetAccountLiveData() {
        return resetAccountLiveData;
    }

    private MutableLiveData<User> tokenLoginPwdLiveData = new MutableLiveData<>();
    private MutableLiveData<User> userInfoLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> updateInfoLiveData = new MutableLiveData<>();
    private MutableLiveData<String> errorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> userInfoErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> resetAccountErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getUserInfoErrorLiveData() {
        return userInfoErrorLiveData;
    }

    public MutableLiveData<User> getUserInfoLiveData() {
        return userInfoLiveData;
    }

    public MutableLiveData<ApiResponse> getUpdateInfoLiveData() {
        return updateInfoLiveData;
    }

    public MutableLiveData<ApiResponse> getRegisterLiveData() {
        return registerLiveData;
    }

    public MutableLiveData<String> getResetAccountErrorLiveData() {
        return resetAccountErrorLiveData;
    }

    public UserViewModel(AccountRepository userRepository) {
        this.userRepository = userRepository;
    }


    public void login(User user) {
        userRepository.login(user, new RepositoryCallback<ApiResponse<LoginResponse>>() {
            @Override
            public void onSuccess(ApiResponse<LoginResponse> data) {
                if (data != null) {
                    userLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void register(User user) {
        userRepository.register(user, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                registerLiveData.setValue(data);
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updatePassword(User user) {
        userRepository.updatePassword(user, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updatePwdLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void removeUser() {
        userRepository.removeUser(new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    resetAccountLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                resetAccountErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void tokenLogin(String token) {
        User user = new User();
        user.setToken(token);
        userRepository.tokenLogin(user, new RepositoryCallback<User>() {
            @Override
            public void onSuccess(User data) {
                if (data != null) {
                    tokenLoginPwdLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updateUserInfo(User user) {
        userRepository.updateUserInfo(user, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updateInfoLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getUserInfoByToken() {
        userRepository.getUserInfoByToken(new RepositoryCallback<ApiResponse<User>>() {
            @Override
            public void onSuccess(ApiResponse<User> data) {
                if (data != null) {
                    userInfoLiveData.setValue(data.getData());
                }
            }

            @Override
            public void onError(Exception e) {
                userInfoErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public MutableLiveData<User> getTokenLoginPwdLiveData() {
        return tokenLoginPwdLiveData;
    }

    public LiveData<ApiResponse<LoginResponse>> getUserLiveData() {
        return userLiveData;
    }

    public LiveData<String> getErrorLiveData() {
        return errorLiveData;
    }
}
